// 打开 GPIOF 的时钟使能
//   RCC->IOPENR |= RCC_IOPENR_GPIOFEN;

//   GPIOF->MODER |= GPIO_MODER_MODE0_0;     /* Set GPIO mode to output */
//   GPIOF->MODER &= ~GPIO_MODER_MODE0_1;    /* Set GPIO mode to output */
//   GPIOF->OTYPER &= ~GPIO_OTYPER_OT0;      /* Set GPIO type to push-pull */
//   GPIOF->OSPEEDR |= GPIO_OSPEEDR_OSPEED0; /* Set GPIO speed to high */
//   GPIOF->ODR &= ~GPIO_ODR_OD0;

//   GPIOA->MODER |= GPIO_MODER_MODE7_0;     /* Set GPIO mode to output */
//   GPIOA->MODER &= ~GPIO_MODER_MODE7_1;    /* Set GPIO mode to output */
//   GPIOA->OTYPER &= ~GPIO_OTYPER_OT7;      /* Set GPIO type to push-pull */
//   GPIOA->OSPEEDR |= GPIO_OSPEEDR_OSPEED7; /* Set GPIO speed to high */
//   GPIOA->ODR &= ~GPIO_ODR_OD7;

//   GPIOA->MODER |= GPIO_MODER_MODE6_0;     /* Set GPIO mode to output */
//   GPIOA->MODER &= ~GPIO_MODER_MODE6_1;    /* Set GPIO mode to output */
//   GPIOA->OTYPER &= ~GPIO_OTYPER_OT6;      /* Set GPIO type to push-pull */
//   GPIOA->OSPEEDR |= GPIO_OSPEEDR_OSPEED6; /* Set GPIO speed to high */
//   GPIOA->ODR &= ~GPIO_ODR_OD6;

// #include <math.h>
#include "digit_tube.h"

void Digit_Tube_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    // 使能 GPIOB 和 GPIOF 时钟
    __HAL_RCC_GPIOB_CLK_ENABLE();
    __HAL_RCC_GPIOF_CLK_ENABLE();

    // 初始化数码管使能引脚
    GPIO_InitStruct.Pin = Digit_Tube0_En | Digit_Tube3_En;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = Digit_Tube1_En | Digit_Tube2_En;
    HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

    // 初始化数码管段选引脚
    GPIO_InitStruct.Pin = Digit_TubeA_Pin | Digit_TubeB_Pin | Digit_TubeC_Pin | Digit_TubeD_Pin |
                          Digit_TubeE_Pin | Digit_TubeF_Pin | Digit_TubeG_Pin | Digit_TubeDP_Pin;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

void Digit_Tube_Close(void)
{
    // 关闭所有数码管
    HAL_GPIO_WritePin(GPIOB, Digit_Tube0_En | Digit_Tube3_En, GPIO_PIN_SET);
    HAL_GPIO_WritePin(GPIOF, Digit_Tube1_En | Digit_Tube2_En, GPIO_PIN_SET);

    // 关闭所有段选
    HAL_GPIO_WritePin(GPIOA, Digit_TubeA_Pin | Digit_TubeB_Pin | Digit_TubeC_Pin | Digit_TubeD_Pin |
                      Digit_TubeE_Pin | Digit_TubeF_Pin | Digit_TubeG_Pin | Digit_TubeDP_Pin, GPIO_PIN_SET);
}

void Digit_Tube_All_Display(u8 num)
{
    // 显示四位数字
    for (u8 i = 0; i < 5; i++)
    {
        // u8 digit = (num / (u16)pow(10, 3 - i)) % 10;
        Digit_Tube_Display(i, num);
        HAL_Delay(1);
    }
}

void Digit_Tube_Display(u16 tube, u8 num)
{
    // 关闭所有数码管
    HAL_GPIO_WritePin(GPIOB, Digit_Tube0_En | Digit_Tube3_En, GPIO_PIN_SET);
    HAL_GPIO_WritePin(GPIOF, Digit_Tube1_En | Digit_Tube2_En, GPIO_PIN_SET);

    // 使能对应的数码管
    switch (tube)
    {
    case 0:
        HAL_GPIO_WritePin(GPIOB, Digit_Tube0_En, GPIO_PIN_RESET);
        break;
    case 1:
        HAL_GPIO_WritePin(GPIOF, Digit_Tube1_En, GPIO_PIN_RESET);
        break;
    case 2:
        HAL_GPIO_WritePin(GPIOF, Digit_Tube2_En, GPIO_PIN_RESET);
        break;
    case 3:
        HAL_GPIO_WritePin(GPIOB, Digit_Tube3_En, GPIO_PIN_RESET);
        break;
    default:
        break;
    }

    // 关闭所有段选
    HAL_GPIO_WritePin(GPIOA, Digit_TubeA_Pin | Digit_TubeB_Pin | Digit_TubeC_Pin | Digit_TubeD_Pin | Digit_TubeE_Pin | Digit_TubeF_Pin | Digit_TubeG_Pin | Digit_TubeDP_Pin, GPIO_PIN_SET);
    // 显示数字
    switch (num)
    {
    case 0:
        HAL_GPIO_WritePin(GPIOA, Digit_TubeA_Pin | Digit_TubeB_Pin | Digit_TubeC_Pin | Digit_TubeD_Pin | Digit_TubeE_Pin | Digit_TubeF_Pin, GPIO_PIN_RESET);
        break;
    case 1:
        HAL_GPIO_WritePin(GPIOA, Digit_TubeB_Pin | Digit_TubeC_Pin, GPIO_PIN_RESET);
        break;
    case 2:
        HAL_GPIO_WritePin(GPIOA, Digit_TubeA_Pin | Digit_TubeB_Pin | Digit_TubeD_Pin | Digit_TubeE_Pin | Digit_TubeG_Pin, GPIO_PIN_RESET);
        break;
    case 3:
        HAL_GPIO_WritePin(GPIOA, Digit_TubeA_Pin | Digit_TubeB_Pin | Digit_TubeC_Pin | Digit_TubeD_Pin | Digit_TubeG_Pin, GPIO_PIN_RESET);
        break;
    case 4:
        HAL_GPIO_WritePin(GPIOA, Digit_TubeB_Pin | Digit_TubeC_Pin | Digit_TubeF_Pin | Digit_TubeG_Pin, GPIO_PIN_RESET);
        break;
    case 5:
        HAL_GPIO_WritePin(GPIOA, Digit_TubeA_Pin | Digit_TubeC_Pin | Digit_TubeD_Pin | Digit_TubeF_Pin | Digit_TubeG_Pin, GPIO_PIN_RESET);
        break;
    case 6:
        HAL_GPIO_WritePin(GPIOA, Digit_TubeA_Pin | Digit_TubeC_Pin | Digit_TubeD_Pin | Digit_TubeE_Pin | Digit_TubeF_Pin | Digit_TubeG_Pin, GPIO_PIN_RESET);
        break;
    case 7:
        HAL_GPIO_WritePin(GPIOA, Digit_TubeA_Pin | Digit_TubeB_Pin | Digit_TubeC_Pin, GPIO_PIN_RESET);
        break;
    case 8:
        HAL_GPIO_WritePin(GPIOA, Digit_TubeA_Pin | Digit_TubeB_Pin | Digit_TubeC_Pin | Digit_TubeD_Pin | Digit_TubeE_Pin | Digit_TubeF_Pin | Digit_TubeG_Pin, GPIO_PIN_RESET);
        break;
    case 9:
        HAL_GPIO_WritePin(GPIOA, Digit_TubeA_Pin | Digit_TubeB_Pin | Digit_TubeC_Pin | Digit_TubeD_Pin | Digit_TubeF_Pin | Digit_TubeG_Pin, GPIO_PIN_RESET);
        break;
    default:
        break;
    }
}

void Digit_Tube_Display_DP(u16 tube)
{
    // 关闭所有数码管
    HAL_GPIO_WritePin(GPIOB, Digit_Tube0_En | Digit_Tube3_En, GPIO_PIN_SET);
    HAL_GPIO_WritePin(GPIOF, Digit_Tube1_En | Digit_Tube2_En, GPIO_PIN_SET);

    // 使能对应的数码管
    switch (tube)
    {
    case 0:
        HAL_GPIO_WritePin(GPIOB, Digit_Tube0_En, GPIO_PIN_RESET);
        break;
    case 1:
        HAL_GPIO_WritePin(GPIOF, Digit_Tube1_En, GPIO_PIN_RESET);
        break;
    case 2:
        HAL_GPIO_WritePin(GPIOF, Digit_Tube2_En, GPIO_PIN_RESET);
        break;
    case 3:
        HAL_GPIO_WritePin(GPIOB, Digit_Tube3_En, GPIO_PIN_RESET);
        break;
    default:
        break;
    }
}